import routes from "./routes";
import { createRouter, createWebHistory } from "vue-router";
import { useMaterialStore } from "@/stores/useMaterial";
import type { IBaseComsUnions } from "@/types/store";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// - 全局后置守卫
router.afterEach((to) => {
  // - 如果当前进入组件市场页，则记录当前进入组件市场页
  const activeView = localStorage.getItem("activeView");
  if (activeView === "materials") {
    useMaterialStore().setCurrentComJsonSchemaName(to.name as IBaseComsUnions);
  }
});

export default router;
